﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lib.Bases;
using Lib.Utils;

namespace MS.INV.MixedDrink
{
    public partial class PhaChe_New : Lib.Bases.Forms.FBaseEdit
    {
        SysObj.Sx_PhaChe order = new SysObj.Sx_PhaChe();

        DataTable DT_HH = new DataTable();

        public PhaChe_New(int _ObjID)
        {
            InitializeComponent();

            this._BaseClass = order;
            this.ObjID = _ObjID;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            try
            {
                using (SysObj.L_HangHoa hh = new SysObj.L_HangHoa())
                    DT_HH = hh.LoadData();

                using (SysLst.L_DVT dvt = new SysLst.L_DVT())
                {
                    DataTable _DT = dvt.LoadData();
                    repDVT.SetDataSource(_DT.Copy(), "TenDVT#Tên ĐVT");
                }

                libpopHH.SetDataSource(DT_HH.Copy(), "MaHang,TenHang#Mã hàng,Tên hàng");

                if (this.ObjID > 0)
                {
                    order.IdPhaChe = this.ObjID;
                    order.ReadData();

                    dtNgay.EditValue = order.Ngay;
                    txtSo.Text = order.So;
                    txtDescr.Text = order.DienGiai;

                    gridNL.DataSource = order.GetProduct();
                }
                else T_NewItemClick(null, null);
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Form load.");
            }
        }

        public override void T_NewItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridNL.DataSource = order.GetProduct();
            txtSo.Text = order.GetNo(Convert.ToDateTime(dtNgay.EditValue));
        }

        public override void T_UpdateItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (txtSo.Text.Trim().Length <= 0)
                {
                    Msg.ErrOK("Vui lòng nhập số chứng từ.");
                    return;
                }
                if (object.Equals(dtNgay.EditValue, null))
                {
                    Msg.ErrOK("Vui lòng chọn ngày lập chứng từ.");
                    return;
                }

                order.So = txtSo.Text;
                order.Ngay = Convert.ToDateTime(dtNgay.EditValue);
                order.DienGiai = txtDescr.Text;

                order.D_TP = gridNL.GetAcceptChanges();

                base.T_UpdateItemClick(sender, e);
            }
            catch (Exception ex)
            {
                SysError.RaiseError(ex, this.Name, "Update data");
            }
        }

        public override void T_DelItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                base.T_DelItemClick(sender, e);
            }
            catch (Exception ex)
            {
                SysError.RaiseError(ex, this.Name, "Delete data");
            }
        }

        private void dtNgay_EditValueChanged(object sender, EventArgs e)
        {
            if (order.IdPhaChe <= 0)
            {
                txtSo.Text = order.GetNo(Convert.ToDateTime(dtNgay.EditValue));
            }
        }

        private void viewNL_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            DataRow row = ((DevExpress.XtraGrid.Views.Grid.GridView)sender).GetFocusedDataRow();
            if (object.Equals(row, null)) return;

            if (e.Column.FieldName.ToLower() == "idhanghoa")
            {
                DataRow[] rS = DT_HH.Select("IdHangHoa=" + Cvert.ToInt32(row["IdHangHoa"], 0));
                if (!object.Equals(rS, null) && rS.Length > 0)
                {
                    row["IdDVT"] = Cvert.ToInt32(rS[0]["IdDVT"], 0);
                }
            }
        }
    }
}
